#!/bin/env bash
source ${AFHOME}/config
TitleText=$1
function CheckFile(){
if [ ! -e $1 ]
  $(TUISoftware) --title "${TitleText}" \
  --msgbox "文件不存在" \
  $(MsgboxSize)
  exit
fi
}

Number=$($(TUISoftware) \
--title "${TitleText}" \
--menu "$(MenuTips)" \
$(MenuSize) \
"1" "修改账号" \
"2" "修改签名" \
"3" "修改主人" \
"0" "  返回  " \
3>&1 1>&2 2>&3)
case ${Number} in
1)
  case $(jq .name package.json) in
  "trss-yunzai")
    $(TUISoftware) --title "${TitleText}" \
    --msgbox "TRSS-Yunzai 暂无法修改" \
    $(MsgboxSize)
    exit
    ;;
  esac
  ConfigFile="config/config/qq.yaml"
  CheckFile ${ConfigFile}
  OldQQ="$(grep qq: ${ConfigFile})"
  OldPwd="$(grep pwd: ${ConfigFile})"
  OldPlatform="$(grep platform: ${ConfigFile})"
  if ! NewQQ=$($(TUISoftware) --title "${TitleText}" \
  --yes-button "确认" \
  --no-button "返回" \
  --inputbox "请输入您的QQ账号" \
  $(MsgboxSize) \
  3>&1 1>&2 2>&3)
  then
    exit 0
  fi
  if ! OldPwd=$($(TUISoftware) --title "${TitleText}" \
  --yes-button "确认" \
  --no-button "返回" \
  --inputbox "请输入您的QQ密码" \
  $(MsgboxSize) \
  3>&1 1>&2 2>&3)
  then
    exit 0
  fi
  NewPlatform=$($(TUISoftware) \
  --title "${TitleText}" \
  --menu "$(MenuTips)" \
  $(MenuSize) \
  "1" "安卓手机" \
  "2" "aPad" \
  "3" "安卓手表" \
  "4" "MacOS" \
  "5" "iPad" \
  "6" "Tim" \
  3>&1 1>&2 2>&3)
  if $(TUISoftware) --title "${TitleText}" \
  --yes-button "确认" \
  --no-button "返回" \
  --inputbox "QQ: ${NewQQ}\n密码: ${OldPwd}" \
  $(MsgboxSize)
  then
    sed -i "s|${OldQQ}|qq: ${NewQQ}|g" ${ConfigFile}
    sed -i "s|${OldPwd}|pwd: ${OldPwd}|g" ${ConfigFile}
    sed -i "s|${OldPlatform}|platform: ${NewPlatform}|g" ${ConfigFile}
  else
    exit 0
  fi
;;
2)
  case $(jq .name package.json) in
  "trss-yunzai")
    $(TUISoftware) --title "${TitleText}" \
    --msgbox "TRSS-Yunzai 暂无法修改" \
    $(MsgboxSize)
    exit
    ;;
  esac
  ConfigFile="config/config/bot.yaml"
  CheckFile ${ConfigFile}
  if NewSignApiAddr=$($(TUISoftware) --title "${TitleText}" \
  --yes-button "确认" \
  --no-button "返回" \
  --inputbox "请输入您的签名服务器链接" \
  $(MsgboxSize) \
  3>&1 1>&2 2>&3)
  then
    OldSignApiAddr="$(grep sign_api_addr ${ConfigFile})"
    OldSignApiAddr="$(grep sign_api_addr ${ConfigFile})"
    NewSignApiAddr=$(echo ${NewSignApiAddr} | sed "s|sign_api_addr: ||g")
    sed -i "s|${OldSignApiAddr}|sign_api_addr: ${NewSignApiAddr}|g" ${ConfigFile}
    API=$(grep sign_api_addr ${ConfigFile} | sed "s|sign_api_addr: ||g")
    $(TUISoftware) --title "${TitleText}" \
    --msgbox "签名API已习惯为\n${API}" \
    $(MsgboxSize)
  else
    exit
  fi
;;
3)
  ConfigFile="config/config/other.yaml"
  CheckFile ${ConfigFile}
  if MasterQQ=$($(TUISoftware) --title "${TitleText}" \
  --yes-button "确认" \
  --no-button "返回" \
  --inputbox "请输入您的主人账号" \
  $(MsgboxSize) \
  3>&1 1>&2 2>&3)
  then
    if echo ${MasterQQ} | grep -qE '^[0-9]+$'
    then
      sed -i "/masterQQ/a\  - ${MasterQQ}" ${ConfigFile}
    else
      MasterQQ=\"${MasterQQ}\"
      sed -i "/masterQQ/a\  - ${MasterQQ}" ${ConfigFile}
    fi
  else
    exit 0
  fi
;;
0)
exit
;;
esac